package top.lemna.account.domain;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import top.lemna.account.persistence.AccountDao;
import top.lemna.account.persistence.AccountPo;

@Component
public class AccountRepositoryImpl implements AccountRepository {
    private final AccountDao accountDao;

    // @Autowired
    public AccountRepositoryImpl(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public Account find(Long accountId) {
        Optional<AccountPo> optional = accountDao.findById(accountId);

        AccountPo accountPo = optional.get();

        return AccountMapper.INSTANCE.toDomain(accountPo);

    }

    @Override
    public void save(Account account) {
        Optional<AccountPo> optional = accountDao.findById(account.getId());
        AccountPo accountPo = optional.get();
        AccountMapper.INSTANCE.update(accountPo,account);
        accountDao.save(accountPo);
    }

}
